package cn.jbolt.ec.order.handler;

import cn.jbolt.common.model.*;
import cn.jbolt.ec.retailer.RetailerService;
import com.jfinal.aop.Inject;

/**
 * C型订单处理类 （充值`）
 */
public class TypeC {
    @Inject
    RetailerService retailerService;



    public int handle(int client_id,int retailer_id,int charge_id,int type){

        /*
        生成新订单
         */
        BecRetailerOrder becRetailerOrder = new BecRetailerOrder();
        BecMember member = new BecMember().dao().findById(client_id);
        becRetailerOrder.setAddress("充值").setStatus(5)
                .setMobile(" "+member.getMobile()).setConsignee(" "+member.getNickname())
                .setType(type).setClientId(client_id).setRetailerId(retailer_id);
        long t =System.currentTimeMillis();

        String orderids = "j-"+t+'-'+client_id+'-'+retailer_id;
        becRetailerOrder.setOrderId(orderids);
        if(!becRetailerOrder.save()) return -1; // 订单生成失败
        becRetailerOrder.setChargeId(charge_id);
        becRetailerOrder.setOrderTime(t);

        BecRetailerChargeBonus chargeBonus =
                new BecRetailerChargeBonus().dao().findById(charge_id);
        becRetailerOrder.setAmountTotal((long)chargeBonus.getAmount())
                .setAmountPaid((long)chargeBonus.getAmount())
                .setChargeAmount(chargeBonus.getAmount())
                .setChargeBonus(chargeBonus.getBonus())
                .setCashAmount(chargeBonus.getAmount()).update();


        return becRetailerOrder.getId();


    }

}
